﻿#region Using directives
using System;
using System.ComponentModel;
using System.Web.Script.Services;
using System.Web.Services;

#endregion

namespace ProcolServices {
    /// <summary>
    /// Summary description for ProjectIdeasService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class ProjectIdeasService : WebService {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        public int AddNewProjectIdea(int projectId, int ideaId) {
            var values = new Object[] {projectId, ideaId};
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                } else if (String.IsNullOrEmpty(str))
                    return -2;
            }
            try {
                var newProjectIdea = new ProjectIdea {ProjectId = projectId, IdeaId = ideaId,};
                db.ProjectIdeas.InsertOnSubmit(newProjectIdea);
                db.SubmitChanges();

                return newProjectIdea.ProjectId;
            } catch (InvalidOperationException) {
                return -2;
            }
        }
    }
}